<template>
  <view class="login-container">
    <!-- 设计说明 -->
    <view class="design-notes">
      <text class="note-title">设计说明</text>
      <text class="note-item">1. 多邻国风格的登录界面</text>
      <text class="note-item">2. 响应式布局适配移动设备</text>
      <text class="note-item">3. 表单验证功能</text>
      <text class="note-item">4. 记住密码选项</text>
      <text class="note-item">5. 登录按钮状态反馈</text>
    </view>
    
    <!-- 登录表单 -->
    <view class="login-form">
      <view class="logo-container">
        <image class="logo" src="/static/logo/ahna.png"></image>
      </view>
      <view class="form-title">今天又是适合养生的一天</view>
      
      <view class="input-group">
        <view class="input-item">
          <text class="input-label">账号</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              type="text" 
              v-model="username" 
              placeholder="请输入账号" 
              @blur="validateUsername"
            />
            <text v-if="username" class="input-icon check">✓</text>
          </view>
          <text v-if="errors.username" class="error-tip">{{ errors.username }}</text>
        </view>
        
        <view class="input-item">
          <text class="input-label">密码</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="showPassword ? 'text' : 'password'" 
              v-model="password" 
              placeholder="请输入密码" 
              @blur="validatePassword"
            />
            <text 
              class="password-toggle" 
              @click="showPassword = !showPassword"
            >{{ showPassword ? '隐藏' : '显示' }}</text>
          </view>
          <text v-if="errors.password" class="error-tip">{{ errors.password }}</text>
        </view>
      </view>
      
      <view class="options">
        <label class="remember-pwd">
          <checkbox 
            :checked="rememberPassword" 
            @click="rememberPassword = !rememberPassword"
            color="#58CC02"
            style="transform:scale(0.8)"
          />
          <text>记住密码</text>
        </label>
        <text class="forgot-pwd" @click="goToForgotPassword">忘记密码?</text>
      </view>
      
      <button 
        class="login-btn" 
        :disabled="!isFormValid || isLoading" 
        :class="{'loading': isLoading}"
        @click="handleLogin"
      >
        {{ isLoading ? '登录中...' : '登录' }}
      </button>
      
      <view class="register-link">
        <text>还没有账号？</text>
        <text class="link" @click="goToRegister">立即注册</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      showPassword: false,
      rememberPassword: false,
      isLoading: false,
      errors: {
        username: '',
        password: ''
      }
    }
  },
  computed: {
    isFormValid() {
      return this.username && this.password && !this.errors.username && !this.errors.password
    }
  },
  methods: {
    validateUsername() {
      if (!this.username) {
        this.errors.username = '请输入账号'
      } else if (this.username.length < 4) {
        this.errors.username = '账号长度不能少于4位'
      } else {
        this.errors.username = ''
      }
    },
    validatePassword() {
      if (!this.password) {
        this.errors.password = '请输入密码'
      } else if (this.password.length < 6) {
        this.errors.password = '密码长度不能少于6位'
      } else {
        this.errors.password = ''
      }
    },
    handleLogin() {
      this.validateUsername()
      this.validatePassword()
      
      if (this.isFormValid) {
        this.isLoading = true
        
        // 模拟登录请求
        setTimeout(() => {
          this.isLoading = false
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          })
          // 登录成功后跳转到分析页面
          setTimeout(() => {
            uni.switchTab({
              url: '/pages/analysis/analysis'
            })
          }, 1000)
        }, 1500)
      }
    },
    goToRegister() {
      uni.navigateTo({
        url: '/pages/register/register'
      })
    },
    goToForgotPassword() {
      uni.navigateTo({
        url: '/pages/forgot-password/forgot-password'
      })
    }
  }
}
</script>

<style>
.login-container {
  display: flex;
  min-height: 100vh;
  background-color: #fff;
  position: relative;
}

/* 设计说明样式 */
.design-notes {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
  max-width: 200px;
}

.note-title {
  font-weight: bold;
  font-size: 14px;
  margin-bottom: 5px;
  display: block;
  color: #333;
}

.note-item {
  font-size: 12px;
  color: #666;
  display: block;
  margin-bottom: 3px;
}

/* 登录表单样式 */
.login-form {
  width: 100%;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo-container {
  background-color: #58CC02;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 30px 0 20px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
}

.logo {
  width: 100px;
  height: 100px;
  object-fit: cover;
  border-radius: 50%;
}

.form-title {
  font-size: 28px;
  font-weight: bold;
  color: #4b4b4b;
  margin-bottom: 30px;
}

.input-group {
  width: 100%;
  margin-bottom: 20px;
}

.input-item {
  margin-bottom: 20px;
  position: relative;
}

.input-label {
  font-size: 15px;
  color: #4b4b4b;
  margin-bottom: 8px;
  display: block;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
  width: 100%;
}

.input {
  width: 100%;
  height: 50px;
  border: 2px solid #e5e5e5;
  border-radius: 12px;
  padding: 0 15px;
  font-size: 16px;
  background-color: #f7f7f7;
  transition: all 0.3s ease;
}

.input:focus {
  border-color: #58CC02;
  background-color: #fff;
  outline: none;
  box-shadow: 0 0 0 4px rgba(88, 204, 2, 0.15);
}

.input-icon.check {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #58CC02;
  font-size: 18px;
  font-weight: bold;
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  color: #58CC02;
  font-weight: bold;
}

.error-tip {
  color: #ff4b4b;
  font-size: 13px;
  margin-top: 5px;
  display: block;
  font-weight: 500;
}

.options {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 25px;
}

.remember-pwd {
  display: flex;
  align-items: center;
  font-size: 15px;
  color: #4b4b4b;
}

.forgot-pwd {
  font-size: 15px;
  color: #58CC02;
  font-weight: bold;
}

.login-btn {
  width: 100%;
  height: 55px;
  background-color: #58CC02;
  color: white;
  border: none;
  border-radius: 16px;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 0 #58a700;
  transition: all 0.2s ease;
}

.login-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #58a700;
}

.login-btn.loading {
  opacity: 0.9;
}

.login-btn:disabled {
  background-color: #afafaf;
  box-shadow: 0 4px 0 #8f8f8f;
}

.register-link {
  font-size: 16px;
  color: #4b4b4b;
  margin-top: 10px;
}

.link {
  color: #58CC02;
  margin-left: 5px;
  font-weight: bold;
  text-decoration: underline;
}
</style>